package com.wangxinyuchuan.demo.iterator;

public class BookShelf implements Aggregate<Book>{
    private Book[] books;

    private int last;

    public BookShelf(int maxsize) {
        this.books = new Book[maxsize];
        this.last = 0;
    }

    public Book getBookAt(int index) {
        return books[index];
    }

    public int getLength() {
        return last;
    }

    public void appendBook(Book book) {
        if (last < books.length) {
            books[last] = book;
            last++;
        } else {
            throw new ArrayIndexOutOfBoundsException("超出书架容量，书架容量为：" + books.length);
        }
    }


    @Override
    public Iterator<Book> iterator() {
        return new BookShelfIterator(this);
    }
}
